package com.jcfk.system.controller;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.controller.BaseController;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.system.utils.FineRsaEncrypt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.security.InvalidKeyException;

/**
 * fineReport管理
 */
@Api(value = "fineReport", tags = {"fineReport管理"})
@RestController
@RequestMapping("fineReport")
public class FineReportManageController extends BaseController {
    /**
     * 登录用户
     */
    @Value("${fineReport.userName}")
    private String publicUserName;

    /**
     * 公钥
     */
    @Value("${fineReport.publicKey}")
    private String publicKey;

    /**
     * 生成Token
     * @param userName
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string", name = "userName", value = "", required = true)
    })
    @ApiOperation(value = "生成Token", notes = "生成Token", httpMethod = "POST")
    @PostMapping("/buildSsoToken")
    public RetResult<?> buildSsoToken(@RequestParam(required = false) String userName) throws InvalidKeyException {
        String user=this.publicUserName;
        if(StrUtil.isNotEmpty(userName)){
            user=userName;
        }
        String ssoToken="";
        publicKey=publicKey.replace(" ","\n");
        ssoToken = FineRsaEncrypt.encrypt(user, publicKey);

        return RetResult.ok(ssoToken);
    }
}
